<?php

App::uses('AppController', 'Controller');

class MigratesController extends AppController
{
    public $name = 'Migrates';
    public $uses = array('Occurrence','Ocorrencias', 'UsersOccurrences', 'UsuariosOcorrencias', 'Estabelecimentos', 'Establishment');
    
    function index()
    {
        die('jah feito');
        
        $old_occurrences = $this->Ocorrencias->find('all');
        foreach($old_occurrences as $old_occurrence)
        {
            //echo($old_occurrence['Ocorrencias']['id']);
        
        
            if($old_occurrence['Ocorrencias']['guia_id'] > 0)
            $person_type = 'guia';
            if($old_occurrence['Ocorrencias']['estabelecimento_id'] > 0)
            $person_type = 'juridico';
            if($old_occurrence['Ocorrencias']['fisica_id'] > 0)
            $person_type = 'fisico';
            
            $this->Occurrence->create();
            $occurrences = $this->Occurrence->set(array(
                'origin' => $old_occurrence['Ocorrencias']['origem'],
                'name' => $old_occurrence['Ocorrencias']['nome_reclamante'],
                'cpf' => $old_occurrence['Ocorrencias']['cpf'],
                'telephone' => $old_occurrence['Ocorrencias']['telefone_reclamante'],
                'email' => $old_occurrence['Ocorrencias']['email_reclamante'],
                'date' => $old_occurrence['Ocorrencias']['data'],
                'type' => $old_occurrence['Ocorrencias']['tipo'],
                'description' => $old_occurrence['Ocorrencias']['descricao'],
                'solution' => $old_occurrence['Ocorrencias']['providencia'],
                'date_solution' => $old_occurrence['Ocorrencias']['prazo'],
                'created' => $old_occurrence['Ocorrencias']['created'],
                'guide_id' => $old_occurrence['Ocorrencias']['guia_id'],
                'establishment_id' => $old_occurrence['Ocorrencias']['estabelecimento_id'],
                'individual_id' => $old_occurrence['Ocorrencias']['fisica_id'],
                'status' => $old_occurrence['Ocorrencias']['status'],
                'date_resolved' => $old_occurrence['Ocorrencias']['data_solucionada'],
                'description_solution' => $old_occurrence['Ocorrencias']['descricao_solucionada'],
                'process_number' => $old_occurrence['Ocorrencias']['processo_num'],
                'institution_id' => $old_occurrence['Ocorrencias']['destino'],
                'situation' => $old_occurrence['Ocorrencias']['situacao'],
                'person_type' => $person_type,
            ));
            
            $this->Occurrence->save($occurrences);
        
        }
        echo('salvo');
        
        
        
        
        $old_usersoccurrences = $this->UsuariosOcorrencias->find('all');
        foreach($old_usersoccurrences as $old_useroccurrence)
        {
            $this->UsersOccurrences->create();
            $useroccurrences = $this->UsersOccurrences->set(array(
                'occurrence_id' => $old_useroccurrence['UsuariosOcorrencias']['ocorrencia_id'],
                'user_id' => $old_useroccurrence['UsuariosOcorrencias']['usuario_id'],
                'action' => $old_useroccurrence['UsuariosOcorrencias']['action'],
                'protocol' => $old_useroccurrence['UsuariosOcorrencias']['protocolo']
            ));
            
            $this->UsersOccurrences->save($useroccurrences);
        }
        echo('salvo');
        //die(debug($old_occurrences));
    }
    
    
    function providers()
    {
        die('jah feito');
        $old_occurrences = $this->Estabelecimentos->find('all');
        foreach($old_occurrences as $old_occurrence)
        {
            $this->Establishment->create();
            $occurrences = $this->Establishment->set(array(
                'id' => $old_occurrence['Estabelecimentos']['id'],
                'name' => $old_occurrence['Estabelecimentos']['nome'],
                'fantasy_name' => $old_occurrence['Estabelecimentos']['fantasia'],
                'identify' => $old_occurrence['Estabelecimentos']['identificador'],
                'responsible_person' => $old_occurrence['Estabelecimentos']['responsavel'],
                'telephone' => $old_occurrence['Estabelecimentos']['telefone'],
                'address' => $old_occurrence['Estabelecimentos']['endereco'],
                'latitude' => $old_occurrence['Estabelecimentos']['latitude'],
                'longitude' => $old_occurrence['Estabelecimentos']['longitude'],
                'email' => $old_occurrence['Estabelecimentos']['email'],
                'cnpj' => $old_occurrence['Estabelecimentos']['cnpj'],
                'activity' => $old_occurrence['Estabelecimentos']['atividade'],
                'city' => $old_occurrence['Estabelecimentos']['localidade'],
            ));
            
            $this->Establishment->save($occurrences);
        
        }
        echo('salvo');
    }

}